package py.com.alfa.escriba.gui.rest.services.resttemplate;

import java.io.IOException;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;

import py.com.alfa.escriba.gui.config.EscribaConfigApplicationContextProvider;
import py.com.alfa.escriba.gui.dto.gestion.TemplatesDTO;
import py.com.alfa.escriba.gui.rest.services.TemplatesService;
import py.com.alfa.escriba.gui.services.Services;
import py.com.sif.rest.client.spring.request.ClientRequestBuilder;

@SuppressWarnings({ "unchecked", "unused"})
public class TemplateServiceClient implements TemplatesService {
	
	private Logger log = LoggerFactory.getLogger(getClass());
	
	public TemplateServiceClient() {}

	@Override
	public List<TemplatesDTO> listTemplates() throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		ResponseEntity<TemplatesDTO[]> response = (ResponseEntity<TemplatesDTO[]>) request.exchange(LIST_TEMPLATE, HttpMethod.GET, TemplatesDTO[].class);
		log.info("OK - listTemplates ");
		List<TemplatesDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

	@Override
	public TemplatesDTO createTemplates(TemplatesDTO template) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addBody(template);
		ResponseEntity<TemplatesDTO> response = (ResponseEntity<TemplatesDTO>) request.exchange(LIST_TEMPLATE, HttpMethod.POST, TemplatesDTO.class);
		log.info("OK - createTemplate ");
		TemplatesDTO result = response.getBody();
		return result;
	}

	@Override
	public TemplatesDTO saveTemplates(TemplatesDTO template) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", template.getId().toString());
		request.addBody(template);
		ResponseEntity<TemplatesDTO> response = (ResponseEntity<TemplatesDTO>) request.exchange(MB_TEMPLATE, HttpMethod.PUT, TemplatesDTO.class);
		log.info("OK - saveTemplate ");
		TemplatesDTO result = response.getBody();
		return result;
	}

	@Override
	public void deleteTemplates(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		request.exchange(MB_TEMPLATE, HttpMethod.DELETE, null);
		log.info("OK - deleteTemplate ");

	}

	@Override
	public TemplatesDTO showTemplates(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<TemplatesDTO> response = (ResponseEntity<TemplatesDTO>) request.exchange(MB_TEMPLATE, HttpMethod.GET, TemplatesDTO.class);
		log.info("OK - showTemplate ");
		TemplatesDTO result = response.getBody();
		return result;
	}

	@Override
	public List<TemplatesDTO> listTemplatesByIdTipo(Long id) throws IOException {
		GenericApplicationContext ctx = (GenericApplicationContext) EscribaConfigApplicationContextProvider.getContext();
		ClientRequestBuilder request = (ClientRequestBuilder) ctx.getBean(Services.REST_CLIENT);
		request.addParam("id", id.toString());
		ResponseEntity<TemplatesDTO[]> response = (ResponseEntity<TemplatesDTO[]>) request.exchange(TEMPLATE_BY_TIPO, HttpMethod.GET, TemplatesDTO[].class);
		log.info("OK - listTemplates ");
		List<TemplatesDTO> result = request.getArrayAsList(response.getBody());
		return result;
	}

}
